package com.wanmait.recruit.filter;

import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.service.PersonService;
import com.wanmait.recruit.service.impl.PersonServiceImpl;
import com.wanmait.recruit.util.CookieUtil;
import com.wanmait.recruit.util.Md5Util;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/web/*")
public class PersonAutoLoginFilter implements Filter {

   private PersonService personService;

    @Override//注入service
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext servletContext = filterConfig.getServletContext();
        WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        if(cxt != null && cxt.getBean(PersonService.class) != null && personService == null){
            personService =cxt.getBean(PersonService.class);
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        HttpSession session = request.getSession();
        Person person = (Person) session.getAttribute("person");
        if (person!=null){//已经登录，放行
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        //如果没有登录，需要读取cookie实现自动登录
        String personName =null;
        String password =null;
        try {
            personName = CookieUtil.getValue("personName",request);
            password = CookieUtil.getValue("password",request);
        }catch (Exception e){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if ("".equals(personName)||"".equals(password)||personName==null||password==null){//cookie过期，放行
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        //检查登录  existence存在
        boolean personExistence = personService.selectIsPersonExistence(personName);
        if (personExistence==false){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        Person byPersonName = personService.findByPersonName(personName);

        //比较密码
        String rightPassword = byPersonName.getPassword();
        rightPassword = Md5Util.md5Super(rightPassword);
        if (rightPassword.equals(password)){//登录成功
            session.setAttribute("person",byPersonName);
            //cookie延期30天
            CookieUtil.addCookie("personName", byPersonName.getPersonName(), "/", 30*24*60*60, response);
            CookieUtil.addCookie("password", rightPassword, "/", 30*24*60*60, response);
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }


}
